import axios from "axios";
import { Message } from "element-ui";
import VueRouter from "../router/index";
import store from "@/store";
import md5 from "js-md5";
import nProgress from "nprogress";
const service = axios.create({
  baseURL: process.env.VUE_APP_BASE_API,
  timeout: 40000000000,
});
service.interceptors.request.use(
  (config) => {
    let token = window.sessionStorage.getItem("token");
    if (token) {
      config.headers["token"] = token;
      // config.headers['P3P'] = "store.getters.token"
      // config.headers['Set-Cookie'] = "cookieName=cookieValue; path=/; secure; HttpOnly; SameSite=None"
      // config.headers['Set-Cookie'] = 'SameSite=None'
      // config.headers['X-Frame-Options'] = "SAMEORIGIN"
    }
    return config;
  },
  (error) => {
    console.log(error);
    return Promise.reject(error);
  }
);

service.interceptors.response.use(
  (response) => {
    // response.setHeader("X-Frame-Options", "SAMEORIGIN");
    const res = response.data;

    if (res.code == "500") {
      // Message({
      //     message: res.msg || "Error",
      //     type: "error",
      //     duration: 5 * 1000
      // });
      setTimeout(() => {
        // VueRouter.push("/login");
        VueRouter.push({
          name: "login",
        });
      }, 500);
      return Promise.reject(new Error(res.msg || "Error"));
    } else {
      // Promise.reject(new Error(res.msg || 'Error'))
      return res;
    }
  },
  (error) => {
    return Promise.reject(error);
  }
);
export default service;
